package com.ukao.steward.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.WindowManager;
import android.widget.TextView;

import com.ukao.steward.R;
import com.ukao.steward.retrofit.ApiClient;
import com.ukao.steward.retrofit.ApiStores;
import com.ukao.steward.ui.login.LoginActivity;
import com.ukao.steward.ui.main.MainActivity;
import com.ukao.steward.util.AppManager;
import com.ukao.steward.util.T;
import com.ukao.steward.widget.ZProgressHUD;

import org.greenrobot.eventbus.EventBus;
import org.zackratos.ultimatebar.UltimateBar;



import me.yokeyword.fragmentation.SupportActivity;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;



/**
 * Created by Caodongyao on 2017/7/31.
 */

public abstract  class BaseActivity extends SupportActivity {

    private ZProgressHUD progressDialog;
    private CompositeSubscription mCompositeSubscription;
    protected Resources resources;
    protected  SupportActivity  _mActivity;
    protected  boolean useEventBus(){
        return false;
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resources= getResources();
         _mActivity = this;
        AppManager.getAppManager().addActivity(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        resetDensity();
        if(useEventBus()){
            EventBus.getDefault().register(this);//订阅
        }
    }
    protected abstract void initView();





    protected void initListener() {
    }


    /**
     * 暴力适配界面
     */
    public final static float DESIGN_WIDTH = 720;
    public void resetDensity(){
        Point size = new Point();
        ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getSize(size);
        getResources().getDisplayMetrics().xdpi = size.x/DESIGN_WIDTH*72f;
    }


//    @Override
//    protected void onResume() {
//        super.onResume();
//        if (this.getClass().getCanonicalName()!=null)
//            JAnalyticsInterface.onPageStart(getApplicationContext(), this.getClass().getCanonicalName());
//
//    }
//
//
//    @Override
//    protected void onPause() {
//        super.onPause();
//        if (this.getClass().getCanonicalName()!=null)
//        JAnalyticsInterface.onPageEnd(getApplicationContext(),this.getClass().getCanonicalName());
//    }

    public  void  showErrors(String  error){
        T.show(error);
    }
    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);

        if (intent != null && intent.getComponent() != null) {
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }
    @Override
    public void finish() {
        super.finish();
        if (!((Object) this).getClass().equals(MainActivity.class) ) {
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    }





    public ZProgressHUD showProgressDialog( ) {
        if (progressDialog==null){
            progressDialog= ZProgressHUD.getInstance(this);
        }
        progressDialog.show();
        return progressDialog;
    }

    public ZProgressHUD showProgressDialog(String message) {
        if (progressDialog!=null){
            progressDialog= ZProgressHUD.getInstance(this);
        }
        progressDialog.setMessage(message);
        progressDialog.show();
        return progressDialog;
    }

    public void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {

            progressDialog.dismiss();
        }
    }
    public ApiStores apiStores() {
        return ApiClient.retrofit().create(ApiStores.class);
    }


    public void onUnsubscribe() {
        //取消注册，以避免内存泄露
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions())
            mCompositeSubscription.unsubscribe();
    }


    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onUnsubscribe();
        mCompositeSubscription=null;
        AppManager.getAppManager().finishActivity(this);
        if(useEventBus()){EventBus.getDefault().unregister(this);//解除订阅
        }

    }
    public void initSystemBarTint() {
        UltimateBar ultimateBar = new UltimateBar(this);
        ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.colorPrimary));
    }

    public  int  getColors( @ColorRes int id){
       return   ContextCompat.getColor(this,id);

    }

    // 设置状态栏颜色
    public void setStatusBarTintColor(int resId) {
        UltimateBar ultimateBar = new UltimateBar(this);
        ultimateBar.setColorBar(ContextCompat.getColor(this,resId));

    }

    protected void initLinearRecyclerView(RecyclerView recyclerViews ) {
        recyclerViews.setHasFixedSize(true);
        recyclerViews.setLayoutManager(new LinearLayoutManager(this));


    }
    protected String getText(TextView txt) {
        String message = txt.getText().toString().trim();

        return message;
    }

}
